package com.jye.rseven.user.serivce.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.jye.rseven.domain.Classes;
import com.jye.rseven.domain.Credit;
import com.jye.rseven.domain.CreditRecord;
import com.jye.rseven.domain.User;
import com.jye.rseven.user.mapper.*;
import com.jye.rseven.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    ClassMapper classMapper;

    @Autowired
    CoursesMapper coursesMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CreditRecordMapper recordMapper;

    @Autowired
    CreditMapper creditMapper;


    @Override
    public Classes findClassById(int id) {
        Classes classes = classMapper.findClassById(id);
        classes.setCourses(coursesMapper.findCourseById(classes.getCId()));
        return classes;
    }

    @Override
    public List<Classes> findAllClass(int skip,int limit) {
        return classMapper.findAllClass(skip,limit);
    }

    @Override
    public List<Classes> findMyClassesByUserId(int uId,int skip,int limit) {
        List<Integer> cIds = classMapper.findMyClassIdByUserId(uId,skip,limit);
        List<Classes>  list  = new ArrayList<Classes>();
        Classes classes;
        for (Integer cId : cIds) {
            classes = classMapper.findClassById(cId);
            classes.setCourseName(coursesMapper.findCourseNameById(classes.getCId()));
            list.add(classes);
        }
        return list;

    }

    @Override
    public int addClass(Classes classes) {
        try{
            //UUID
            String uuId = UUID.randomUUID().toString();
            String[] split = uuId.split("-");
            StringBuilder s  = new StringBuilder();
            for (String s1 : split) {
                s.append(s1);
            }
            //转换成String
            String shareCode =  s.toString();
            classes.setShareCode(shareCode);
            classMapper.addClass(classes);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int updateClass(Classes classes) {
        try{
            classMapper.updateClass(classes);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public String addUserToClass(int uId, int cId ){
        try{
            //判断有无加入
            int inClass = classMapper.isInClass(uId, cId);
            if(inClass!=0){
                //有记录
                return "用户已经加入班级了!";
            }else{
                //无记录,插入
                classMapper.addUserToClass(uId,cId);
                return "用户加入班级成功!";
            }
        }catch (Exception e){
            return "用户加入班级失败!";
        }
    }


    @Override
    public String addUsersToClass(int[] uIds, int cId) {
        try {
            int inClass;
            for (int uId : uIds) {
                //判断有无加入
                inClass = classMapper.isInClass(uId, cId);
                if (inClass != 0) {
                    //有记录
                    String name = userMapper.findUserNameById(uId);
                    return name + "已经加入班级了!";
                } else {
                    //无记录,插入
                    classMapper.addUserToClass(uId, cId);
                }
            }
            return "操作成功!用户已经加入班级了!";
        }catch(Exception e){
            return "操作失败!";
        }
    }

    @Override
    public List<User> findClassUsersByCId(int cId) {
        List<Integer> ids = classMapper.findUsersIdsByCId(cId);
        List<User> userList = new ArrayList<>();
        for (Integer id : ids) {
            User user = userMapper.findUserById(id);
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<Classes> findClassByCId(int cId) {
        List<Classes> classes = classMapper.findClassByCId(cId);
        return classes;
    }



    @Override
    public List<User> findClassUserById(int id) {
        List<Integer> uIds = classMapper.findUsersIdsByCId(id);
        List<User> userList = new ArrayList<>();
        for (Integer uId : uIds) {
            User user = userMapper.findUserById(uId);
            user.setClassName(classMapper.findClassNameById(id));
            userList.add(user);
        }
        return userList;
    }

    @Override
    public Classes findClassByShareCode(String shareCode) {
        Classes classes = classMapper.findClassByShareCode(shareCode);
        return classes;
    }

    @Override
    public List<Classes> findClassByName(String name) {
        List<Classes> classes = classMapper.findClassByName(name);
        return classes;
    }

    @Override
    public String deleteClassById(int id) {
        try {
            classMapper.deleteClassById(id);
            return "删除班级成功!该班级用户变成未分班状态.";
        }catch (Exception e){
            return "删除失败!";
        }
    }

    @Override
    public String deleteUserToClassByUIdAndCId(int uId, int cId) {
        try{
            classMapper.deleteUserToClassByUIdAndCId(uId, cId);
            return "移除用户成功!";
        }catch (Exception e){
            return "移除用户失败!";
        }
    }

    @Override
    public int isInClass(int uId, int cId) {
        int inClass = classMapper.isInClass(uId, cId);
        return inClass;
    }

    @Override
    public List<User> findClassUserMonthsCreditById(int id) {
        List<Integer> uIds = classMapper.findUsersIdsByCId(id);
        Credit credit;
        User user;
        List<User> userList = new ArrayList<>();
        for (Integer uId : uIds) {
            user = userMapper.findUserById(uId);
            user.setCreditRecord(recordMapper.findMyCreditRecord(uId)==null? new CreditRecord(): recordMapper.findMyCreditRecord(uId) );
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<User> findClassUserCreditById(int id) {
        List<Integer> uIds = classMapper.findUsersIdsByCId(id);
        Credit credit;
        User user;
        List<User> userList = new ArrayList<>();
        for (Integer uId : uIds) {
            user = userMapper.findUserById(uId);
            user.setCredit(creditMapper.findMyCreditByUserId(uId));
            userList.add(user);
        }
        return userList;
    }


}
